<?php

namespace api\modules\app\services;

use api\models\User;
use api\modules\app\traits\TranslateTrait;

class TranslateSetvice
{
    use TranslateTrait;

    private $cacheData = [];

    public function formatDispatchAreaInfo($metaInfo, $perfix = 'from', $cacheData = [])
    {
        if (!empty($cacheData)) {
            $this->cacheData = $cacheData;
        }

        if (empty($metaInfo)) {
            return [];
        }

        return [
            $perfix . '_meta_level1' => $metaInfo['meta_level1'],
            $perfix . '_meta_level1_name' => $this->translateCode($metaInfo['meta_level1'], 'area', $this->cacheData),
            $perfix . '_meta_level2' => $metaInfo['meta_level2'],
            $perfix . '_meta_level2_name' => $this->translateCode($metaInfo['meta_level2'], 'area', $this->cacheData),
            $perfix . '_meta_level3' => $metaInfo['meta_level3'],
            $perfix . '_meta_level3_name' => $this->translateCode($metaInfo['meta_level3'], 'area', $this->cacheData),
            $perfix . '_meta_level4' => $metaInfo['meta_level4'],
            $perfix . '_meta_level4_name' => $this->translateCode($metaInfo['meta_level4'], 'area', $this->cacheData),
            $perfix . '_meta_level5' => $metaInfo['meta_level5'],
            $perfix . '_meta_level5_name' => $this->translateCode($metaInfo['meta_level5'], 'area', $this->cacheData),
            $perfix . '_department' => $metaInfo['department'] ?? '0',
            $perfix . '_department_name' => empty($metaInfo['department']) ? '' : $this->translateCode($metaInfo['department'], 'department'),
            $perfix . '_user_id' => $metaInfo['user_id'],
            $perfix . '_username' => $this->getUserName($metaInfo['user_id']),
        ];
    }

    private function getUserName($id)
    {
        if (empty($id)) {
            return '';
        }
        $user = User::findOne(['id' => $id]);
        return $user->username ?? '';
    }
}